package cn.zhouchao.service.impl;

import cn.zhouchao.domain.Product;
import cn.zhouchao.domain.Store;
import cn.zhouchao.dto.ProductDto;
import cn.zhouchao.feign.StoreService;
import cn.zhouchao.mapper.ProductMapper;
import cn.zhouchao.service.IProductService;
import cn.zhouchao.utils.AjaxResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private StoreService storeService;


    @Override
    public AjaxResult add(ProductDto productDto) {
        try {
            Product product = new Product();
            BeanUtils.copyProperties(productDto, product);
            productMapper.add(product);
            Store store = new Store();
            store.setProductId(product.getId());
            store.setNumber(productDto.getNumber());
            AjaxResult add = storeService.add(store);
            return add;
        } catch (BeansException e) {
            e.printStackTrace();
            return new AjaxResult("新增失败!!!", false, e.getMessage());
        }
    }
}
